package com.roc.admin.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.roc.admin.domain.entity.User;
import com.roc.admin.domain.repository.UserRepository;
import com.roc.admin.infrastructure.mapper.UserMapper;
import com.roc.admin.infrastructure.persistence.UserPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户仓储实现类
 */
@Repository
public class UserRepositoryImpl implements UserRepository {

    private final UserMapper userMapper;

    public UserRepositoryImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User save(User user) {
        UserPO userPO = toUserPO(user);
        if (userPO.getId() == null) {
            userMapper.insert(userPO);
        } else {
            userMapper.updateById(userPO);
        }
        return toUser(userPO);
    }

    @Override
    public Optional<User> findById(Long id) {
        UserPO userPO = userMapper.selectById(id);
        return Optional.ofNullable(userPO).map(this::toUser);
    }

    @Override
    public Optional<User> findByUsername(String username) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getUsername, username);
        UserPO userPO = userMapper.selectOne(queryWrapper);
        return Optional.ofNullable(userPO).map(this::toUser);
    }

    @Override
    public List<User> findAll() {
        List<UserPO> userPOList = userMapper.selectList(null);
        return userPOList.stream().map(this::toUser).collect(Collectors.toList());
    }

    @Override
    public List<User> findByDeptId(Long deptId) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getDeptId, deptId);
        List<UserPO> userPOList = userMapper.selectList(queryWrapper);
        return userPOList.stream().map(this::toUser).collect(Collectors.toList());
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public boolean existsByUsername(String username) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getUsername, username);
        return userMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 将用户实体转换为持久化对象
     *
     * @param user 用户实体
     * @return 用户持久化对象
     */
    private UserPO toUserPO(User user) {
        if (user == null) {
            return null;
        }
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        return userPO;
    }

    /**
     * 将持久化对象转换为用户实体
     *
     * @param userPO 用户持久化对象
     * @return 用户实体
     */
    private User toUser(UserPO userPO) {
        if (userPO == null) {
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(userPO, user);
        return user;
    }
}
